package com.zjj.lbw.net.nio.nio;

import java.util.Scanner;

import static com.zjj.lbw.net.nio.Const.DEFAULT_PORT;
import static com.zjj.lbw.net.nio.Const.DEFAULT_SERVER_IP;


/**
 * @author zhanglei.zjj
 * 类说明：nio通信客户端
 */
public class NioClient {
    private static NioClientHandle nioClientHandle;

    public static void start() {
        nioClientHandle = new NioClientHandle(DEFAULT_SERVER_IP, DEFAULT_PORT);
        //nioClientHandle = new NioClientHandle(DEFAULT_SERVER_IP,8888);
        new Thread(nioClientHandle, "client").start();
    }

    // 向服务器发送消息
    public static boolean sendMsg(String msg) throws Exception {
        nioClientHandle.sendMsg(msg);
        return true;
    }

    public static void main(String[] args) throws Exception {
        start();
        Scanner scanner = new Scanner(System.in);
        // 这里无需考虑写数据时通道是否建立的问题，如果写数据时通道尚未建立，则通道建立完，仍然可以继续写入
        // 这也是非阻塞的一个体现，当前线程无需阻塞等待 完全写入成功
        while (NioClient.sendMsg(scanner.next())) ;

    }

}
